/****************************************************************************
 * 2025.4 LAPTOP-OMJU83NA
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace Survivor
{
	public partial class UnlockedAbilityPanel : UIElement,IController
	{
		private ResLoader _resLoader;
		private SpriteAtlas _iconAtlas;

		public HashSet<string> unlockedAbilityIcons = new();
		private void Awake()
		{
			_resLoader = ResLoader.Allocate();
			_iconAtlas = _resLoader.LoadSync<SpriteAtlas>("Icon");
		}

		private void Start()
		{
			InitUI();
		}

		public void InitUI()
		{
			var items = this.GetSystem<ExpUpgradeSystem>().items;
			var funAddIcon = new Action<ExpUpgradeItem>((item) =>
			{
				if (unlockedAbilityIcons.Add(item.Key))
				{
					UnlockedItemTemplete.InstantiateWithParent(UnlockedItemRoot).Show().Self((self) =>
					{
						self.sprite = _iconAtlas.GetSprite(item.IconName);
					});
				}
			});
			foreach (var item in items)
			{
				if (item.Level > 0)
				{
					funAddIcon.Invoke(item);
				}
				else
				{
					item.OnLevelUp += (value) =>
					{
						if (value > 0)
						{
							funAddIcon.Invoke(item);
						}
					};
				}
			}
		}


		protected override void OnBeforeDestroy()
		{
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}